/**
 * 
 */
package wong.board.client.ui;

import wong.board.client.util.JSONTool;
import wong.board.client.util.JSTool;
import wong.board.shared.ContentKeys;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.Window.Navigator;
import com.google.gwt.user.client.ui.HasText;

/**
 * @author wong
 * 
 */
public class ValidationPresenter extends Presenter<ValidationPresenter.View, JSONObject> {

	private JSONArray acceptedBrowsers;
	private JSONArray validBrowsers;

	public ValidationPresenter() {
		this((View) GWT.create(View.class));
	}

	public ValidationPresenter(View view) {
		super(view);
	}

	public void updateView(JSONObject validation) {
		//
		getView().getUserAgentText().setText(Navigator.getUserAgent());
		getView().getAppText().setText(
				Navigator.getAppName() + " v" + Navigator.getAppVersion() + " {" + Navigator.getAppCodeName()
						+ "}");
		getView().getSystemText().setText(JSTool.getNavigatorOsCpu() + "@" + Navigator.getPlatform());
		//
		View.State state = View.State.Undefined;

		JSONArray newAcceptedBrowsers = JSONTool.isArray(validation, ContentKeys.ValidationAcceptedBrowsers);
		if (newAcceptedBrowsers != null) {
			if (acceptedBrowsers == null) {
				acceptedBrowsers = newAcceptedBrowsers;
			} else {
				acceptedBrowsers = JSONTool.merge(acceptedBrowsers, newAcceptedBrowsers, true);
			}
		}

		if (acceptedBrowsers != null) {
			boolean isAccepted = isOneInUserAgent(acceptedBrowsers);
			if (isAccepted) {
				boolean isValid = false;
				JSONArray newValidBrowsers = JSONTool.isArray(validation, ContentKeys.ValidationValidBrowsers);
				if (newValidBrowsers != null) {
					if (validBrowsers == null) {
						validBrowsers = newValidBrowsers;
					} else {
						validBrowsers = JSONTool.merge(validBrowsers, newValidBrowsers, true);
					}
				}
				if (validBrowsers != null) {
					isValid = isOneInUserAgent(validBrowsers);
					if (isValid) {
						state = View.State.Valid;
					} else {
						state = View.State.Other;
					}
				}
			} else {
				state = View.State.Invalid;
			}
		}

		getView().getStatusText().setText(state.text);
		getView().setState(state);
	}

	private boolean isOneInUserAgent(JSONArray array) {
		String userAgent = Navigator.getUserAgent();
		for (int i = 0; i < array.size(); i++) {
			if (userAgent.contains(array.get(i).isString().stringValue())) {
				return true;
			}
		}
		return false;
	}

	public interface View extends Presenter.View {

		public enum State {
			Undefined("UNDEFINED"), Valid("Valid"), Invalid("INVALID"), Other("Accepted");

			/**/
			private final String text;

			private State(String text) {
				this.text = text;
			}

			@Override
			public String toString() {
				return text;
			}
		}

		public HasText getUserAgentText();

		public HasText getAppText();

		public HasText getSystemText();

		public HasText getStatusText();

		public void setState(State state);
	}

}
